// pages/enterprise/home/home.js
var app = getApp();
import { ajaxPost, showToast } from "../../../utils/util.js";
Page({
    /**
     * 页面的初始数据
     */
    data: {
        page: 0,
        limit: 10,
        selectVal: app.globalData.meetingSelectVal,
        noGet: false,
        listSource: []
    },
    clickSelect(e) {
        if (e.currentTarget.dataset.type !== this.data.selectVal) {
            this.setData({
                page: 0,
                noGet: false,
                selectVal: e.currentTarget.dataset.type
            });
            this.getList(true);
        }
    },
    getList(type) {
        let _this = this;
        if (_this.data.noGet) {
            return;
        }
        let data = {
            token: wx.getStorageSync("token"),
            page: ++_this.data.page,
            limit: _this.data.limit,
            source: "wx",
            type: _this.data.selectVal,
            is_follow: 1
        };
        ajaxPost("/meeting/meetinglist", data, function(res) {
            if (res.code == 0) {
                let data = res.data;
                if (type) {
                    console.log(type);
                    _this.setData({
                        noGet: false,
                        listSource: data
                    });
                    return;
                }
                if (data.length == 0) {
                    _this.setData({
                        noGet: true
                    });
                } else {
                    let newarr = _this.data.listSource.concat(data);
                    _this.setData({
                        listSource: newarr
                    });
                }
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: "none",
                    duration: 2000
                });
            }
        });
    },
    goCont(e) {
        let index = e.currentTarget.dataset.index;
        let data = this.data.listSource[index];
        if (
            (data.status == 1 || data.status == 2) &&
            this.data.selectVal == 1
        ) {
            wx.navigateTo({
                url:
                    "/pages/student/hallRoom/hallRoom?id=" +
                    data.id +
                    "&roomid=" +
                    data.roomid
            });
        }
        if (data.status !== 3 && this.data.selectVal == 2) {
            let studentInfo = this.data.listSource[index];
            wx.navigateTo({
                url: "/pages/student/code/code?id=" + studentInfo.id
            });
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {},

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.setData({
            page: 0
        });
        this.getList(true);
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getList();
    },
    /**
     * 关注按钮
     */
    clickFollowed(e) {
        const index = e.currentTarget.dataset.index;
        let newListSource = this.data.listSource;
        let ajaxData = {
            token: wx.getStorageSync("token"),
            meeting_id: newListSource[index].id
        };
        ajaxPost("/meeting/cancelfollowmeeting", ajaxData, res => {
            if (res.code == 0) {
                newListSource.splice(index, 1);
                this.setData({
                    listSource: newListSource
                });
                showToast("success", res.msg);
            } else {
                showToast("text", res.msg);
            }
        });
    }
});
